package com.apps.ftapp.util;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/8/12.
 */

public class Tools {


    /**
     * 定义script的正则表达式
     */
    private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    /**
     * 定义style的正则表达式
     */
    private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    /**
     * 定义HTML标签的正则表达式
     */
    private static final String REGEX_HTML = "<[^>]+>";
    /**
     * 定义空格回车换行符
     */
    private static final String REGEX_SPACE = "\\s*|\t|\r|\n";


    public static String delHTMLTag(String htmlStr) {
        // 过滤script标签
        Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll("");
        // 过滤style标签
        Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll("");
        // 过滤html标签
        Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll("");
        // 过滤空格回车标签
        Pattern p_space = Pattern.compile(REGEX_SPACE, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll("");
        htmlStr = htmlStr.replace("&nbsp;", "");
        return htmlStr.trim(); // 返回文本字符串
    }


    /**
     * @return
     */
    public static String getTime() {

        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳

        String str = String.valueOf(time);

        return str;

    }

    public static String getTime1() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }


    public static String getcha(String startTime, String endTime) {
        L.i("main", startTime + "::::" + endTime);
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long between = 0;
        try {
            java.util.Date begin = dfs.parse(startTime);
            java.util.Date end = dfs.parse(endTime);
            between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);
        System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
                + "毫秒");

        String newHour = hour + "";
        if (hour < 10)
            newHour = "0" + hour;

        String newMin = min + "";
        if (min < 10)
            newMin = "0" + min;

        String newS = s + "";
        if (s < 10)
            newS = "0" + s;

        return newHour + ":" + newMin + ":" + newS;
    }


    /**
     * round 方法
     * <p>方法说明:
     * 提供精确的小数位四舍五入处理。
     * </p>
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return double
     * @author LiuXiaoFei
     * @date 2014年9月11日 下午9:10:13
     */
    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The   scale   must   be   a   positive   integer   or   zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    /**
     * @param object
     * @return
     */
    public static int isIntNull(Object object) {
        try {
            return Integer.parseInt(object + "");
        } catch (Exception e) {
//            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @param object
     * @return
     */
    public static boolean isBooleanNull(Object object) {
        try {
            return Boolean.parseBoolean(object + "");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    public static double isDoubleNull(Object object) {
        try {
            return Double.parseDouble(object + "");
        } catch (Exception e) {
            //	e.printStackTrace();
            return 0;
        }
    }

    public static float isFloatNull(Object object) {
        try {
            return Float.parseFloat(object + "");
        } catch (Exception e) {
            //	e.printStackTrace();
            return 0;
        }
    }


    /**
     * isNull 方法
     * <p>方法说明:判断是否为空</p>
     *
     * @return String
     * @author LiuXiaoFei
     * @date 2014年8月13日 下午10:06:28
     */
    public static String isNull(Object object) {
        try {
            if ("null".equals(object)) return "";
            if ("(null)".equals(object)) return "";
            return object == null ? "" : object + "";
        } catch (Exception e) {
            return "";
        }
    }


    /**
     * @param status
     * @return
     */
    public static String getSt(String status) {

        if ("paid".equals(Tools.isNull(status))) {
            return "已支付";
        } else if ("cancelled".equals(Tools.isNull(status))) {

            return "已取消";
        } else {

            return "待支付";
        }

    }



     /*
    获取类型
     */

    public static int getType(String typeName) {

        //String[] typeKey = {"single_choice","choice","uncertain_choice","fill","determine","essay","material","lunshu","mingci","bianxi","jiaoxue","huodong","xiezuo","yuedu"};

        // String[] typeName = {"单选题","多选题","不定项选择题","填空题","判断题","问答题","材料分析","论述","名词解释","辨析","教学设计","活动设计","写作","阅读理解"};

        if ("single_choice".equals(Tools.isNull(typeName))) {

            return 0;


        } else if ("choice".equals(Tools.isNull(typeName))) {


            return 1;

        } else if ("uncertain_choice".equals(Tools.isNull(typeName))) {


            return 2;

        } else if ("fill".equals(Tools.isNull(typeName))) {

            return 3;


        } else if ("determine".equals(Tools.isNull(typeName))) {

            return 4;


        } else if ("essay".equals(Tools.isNull(typeName))) {


            return 5;

        } else if ("material".equals(Tools.isNull(typeName))) {


            return 6;

        } else if ("lunshu".equals(Tools.isNull(typeName))) {


            return 7;

        } else if ("mingci".equals(Tools.isNull(typeName))) {


            return 8;

        } else if ("bianxi".equals(Tools.isNull(typeName))) {


            return 9;

        } else if ("jiaoxue".equals(Tools.isNull(typeName))) {


            return 10;

        } else if ("huodong".equals(Tools.isNull(typeName))) {


            return 11;

        } else if ("xiezuo".equals(Tools.isNull(typeName))) {


            return 12;

        } else if ("yuedu".equals(Tools.isNull(typeName))) {


            return 13;

        } else {
            return 0;
        }


    }

       /*
    获取类型
     */

    public static boolean getisYes(int k) {

        switch (k) {
            case 0:

                return false;

//                break;

            case 1:

                return false;
//                break;
            case 2:

                return false;

//                break;
            case 3:

                return true;
//                break;
            case 4:
                return false;

//                break;
            case 5:

                return true;

//                break;
            case 6:

                return true;

//                break;
            case 7:

                return true;

//                break;
            case 8:

                return true;

//                break;
            case 9:

                return true;

//                break;
            case 10:

                return true;

//                break;
            case 11:

                return true;

//                break;
            case 12:

                return true;

//                break;

            case 13:

                return true;

//                break;


        }

        return false;

//        //String[] typeKey = {"single_choice","choice","uncertain_choice","fill","determine","essay","material","lunshu","mingci","bianxi","jiaoxue","huodong","xiezuo","yuedu"};
//
//        // String[] typeName = {"单选题","多选题","不定项选择题","填空题","判断题","问答题","材料分析","论述","名词解释","辨析","教学设计","活动设计","写作","阅读理解"};
//
//        if("single_choice".equals(Tools.isNull(k))){
//
////单选题 0
//            return false;
//
//
//        }else  if("choice".equals(Tools.isNull(typeName))){
//
////多选题 1
//            return false;
//
//        }else  if("uncertain_choice".equals(Tools.isNull(typeName))){
////不定项选择题 2
//
//            return false;
//
//        }
//        else  if("fill".equals(Tools.isNull(typeName))){
////填空题 3
//            return true;
//
//
//        }
//        else  if("determine".equals(Tools.isNull(typeName))){
//
////判断题 4
//            return false;
//
//
//        }
//        else  if("essay".equals(Tools.isNull(typeName))){
////问答题 5
//
//            return true;
//
//        }else  if("material".equals(Tools.isNull(typeName))){
//
////材料分析 6
//            return true;
//
//        }else  if("lunshu".equals(Tools.isNull(typeName))){
//
////论述 7
//            return true;
//
//        }else  if("mingci".equals(Tools.isNull(typeName))){
//
////名词解释
//            return true;
//
//        }else  if("bianxi".equals(Tools.isNull(typeName))){
//
////辨析
//            return true;
//
//        }else  if("jiaoxue".equals(Tools.isNull(typeName))){
//
////教学设计
//            return true;
//
//        }else  if("huodong".equals(Tools.isNull(typeName))){
//
////活动设计
//            return true;
//
//        }else  if("xiezuo".equals(Tools.isNull(typeName))){
////写作
//
//            return true;
//
//        }else  if("yuedu".equals(Tools.isNull(typeName))){
////阅读理解
//
//            return true;
//
//        }else{
//            return false;
//        }
//
//
//    }
//
//
//
//
//
//
//     /*
//    获取类型
//     */
//
//    public static boolean getisYes(String typeName) {
//
//        //String[] typeKey = {"single_choice","choice","uncertain_choice","fill","determine","essay","material","lunshu","mingci","bianxi","jiaoxue","huodong","xiezuo","yuedu"};
//
//        // String[] typeName = {"单选题","多选题","不定项选择题","填空题","判断题","问答题","材料分析","论述","名词解释","辨析","教学设计","活动设计","写作","阅读理解"};
//
//        if("single_choice".equals(Tools.isNull(typeName))){
//
////单选题
//            return false;
//
//
//        }else  if("choice".equals(Tools.isNull(typeName))){
//
////多选题
//            return false;
//
//        }else  if("uncertain_choice".equals(Tools.isNull(typeName))){
////不定项选择题
//
//            return false;
//
//        }
//        else  if("fill".equals(Tools.isNull(typeName))){
////填空题
//            return true;
//
//
//        }
//        else  if("determine".equals(Tools.isNull(typeName))){
//
////判断题
//            return false;
//
//
//        }
//        else  if("essay".equals(Tools.isNull(typeName))){
////问答题
//
//            return true;
//
//        }else  if("material".equals(Tools.isNull(typeName))){
//
////材料分析
//            return true;
//
//        }else  if("lunshu".equals(Tools.isNull(typeName))){
//
////论述
//            return true;
//
//        }else  if("mingci".equals(Tools.isNull(typeName))){
//
////名词解释
//            return true;
//
//        }else  if("bianxi".equals(Tools.isNull(typeName))){
//
////辨析
//            return true;
//
//        }else  if("jiaoxue".equals(Tools.isNull(typeName))){
//
////教学设计
//            return true;
//
//        }else  if("huodong".equals(Tools.isNull(typeName))){
//
////活动设计
//            return true;
//
//        }else  if("xiezuo".equals(Tools.isNull(typeName))){
////写作
//
//            return true;
//
//        }else  if("yuedu".equals(Tools.isNull(typeName))){
////阅读理解
//
//            return true;
//
//        }else{
//            return false;
//        }


    }


    /**
     * @param index
     * @return
     */
    public static String getType(int index) {

        String[] typeKey = {"single_choice", "choice", "uncertain_choice", "fill", "determine", "essay", "material", "lunshu", "mingci", "bianxi", "jiaoxue", "huodong", "xiezuo", "yuedu"};

        String[] typeName = {"单选题", "多选题", "不定项选择题", "填空题", "判断题", "简答题", "材料分析", "论述", "名词解释", "辨析", "教学设计", "活动设计", "写作", "阅读理解"};

        // String[] typeName = {"单选题","多选题","不定项选择题","填空题","判断题","问答题","材料题"};

        return typeName[index];
    }


    public static String getPrintSize(long size) {
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话，要保留最后1位小数，
            //因此，把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的，先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }


}
